/************************************************************************
Copyright 2003, 2004, 2005, 2006, 2007, 2008 Grid Technology Research Center,
National Institute of Advanced Industrial Science and Technology.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
************************************************************************/

package jp.aist.gtrc.plus.reserve;

import java.util.Calendar;


class ReserveUpdateOperator extends OperatorBase {

	private final ReserveNodeNormalizer normalizer;
	private final CommitOperator commiter;
	private final AbortOperator aborter;

	protected ReserveUpdateOperator(ReserveManager rsvMgr,
			ReserveNodeNormalizer normalizer, AbortOperator aborter) {

		this(rsvMgr, normalizer, null, aborter);

	}

	protected ReserveUpdateOperator(ReserveManager rsvMgr,
			ReserveNodeNormalizer normalizer,
			CommitOperator commiter, AbortOperator aborter) {

		super(rsvMgr);
		this.normalizer = normalizer;
		this.commiter = commiter;
		this.aborter = aborter;

		assert(rsvMgr != null);
		assert(normalizer != null);
		assert(table != null);
		assert(recorder != null);
		assert(aborter != null);

	}

	protected final void commitAfter1stPhaseSuccess() throws ReserveException {

		/*
		 * called after 1st phase success
		 * 	- reserve/cancel/modify/destroy succeeded, waiting commit
		 * 	- you can abort transaction by calling aborter.abort()
		 */
		ReserveOperation currentOpe = getCurrentOperation();
		if (currentOpe != null) {
			ReserveInfo target = currentOpe.getTargetReserveInfo();
			if (target != null) {
				recorder.storeOperation(currentOpe);
				recorder.commit(target);
			}
		} else {
			assert(false);
			recorder.commit(null);
		}

	}

	protected final void commitAfter2ndPhaseSuccess(ReserveId rsvId) throws ReserveException {

		/*
		 * called after 2nd phase success
		 * 	- the transaction committed now! cannot change nothing!
		 */
		if (commiter != null) {
			commiter.commit(rsvId);
		} else {
			assert(false);
		}

	}

	protected final void abortWithoutException(ReserveInfo info) {

		assert(aborter != null);
		try {
			if (info != null) {
				aborter.abort(info.getReserveId());
			} else {
				finishCurrentOperation();
			}
		} catch (ReserveException e) {
			logger.debug("abort failed, ignore to continue", e);
		}

	}

	protected final ReserveNodeSet makeReserveNodeSet(ReserveRequest rsvReq) throws ReserveException {

		return normalizer.makeReserveNodeSet(rsvReq);

	}

	protected final void reallocateNodes(Calendar startTime) throws ReserveException {

		normalizer.reallocateNodes(startTime, true);

	}

}
